/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package SourceCode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import javax.swing.table.DefaultTableModel;
import sun.misc.Sort;

/**
 *
 * @author ssaad
 */
public class LibraryGeneral {

    public static ArrayList<Member> S_Members ;
    public static ArrayList<Book> S_Books ;
    public static ArrayList<Magazine> S_Magazines ;
    public static ArrayList<Copy> S_Copies;

    //define HAshMap for Member(id,member), Book(isbn,Book) Magazine (Issueno, )
    public static HashMap<Integer,Member> Map_Members ;
    public static HashMap<String,Book> Map_Books ;
    public static HashMap<String,Magazine> Map_Magazines ;
    public static HashMap<Integer,Copy > Map_Copies ;



     /*
     * this method must be called in the main method befor use
     */
    public static  void LoadAllInitialData()
    {
        String path;
        path = "C:\\\\Liubrary_Serialization_Data\\Members.txt";
        S_Members = new ArrayList<Member>();
        S_Members.addAll((ArrayList<Member>) SerializationGenerator.getSerialization(path));
        path = "C:\\Liubrary_Serialization_Data\\Books.txt";
        S_Books = new ArrayList<Book>();
        S_Books.addAll((ArrayList<Book>) SerializationGenerator.getSerialization(path));
        path = "C:\\Liubrary_Serialization_Data\\Magazines.txt";
        S_Magazines = new ArrayList<Magazine>();
        S_Magazines.addAll((ArrayList<Magazine>) SerializationGenerator.getSerialization(path));
        path = "C:\\Liubrary_Serialization_Data\\Copy.txt";
        S_Copies = new ArrayList<Copy>();
        S_Copies.addAll((ArrayList<Copy>) SerializationGenerator.getSerialization(path));

        //Load data in hashmaps
        //load Members in hashmap of members
        Map_Members = new HashMap<Integer, Member>();
        for(Member m : S_Members)
        {
            Map_Members.put(m.getMemberID(), m);
        }
        //Load Books in Map_books
        Map_Books = new HashMap<String, Book>();
        for(Book b: S_Books)
        {
            Map_Books.put(b.getISBN(), b);
        }
        //Load Magazines in map_magazines
        Map_Magazines = new HashMap<String, Magazine>();
        for(Magazine mag : S_Magazines )
        {
            Map_Magazines.put(mag.getIssueno(), mag);
        }
        //Load Magazines in map_magazines
        Map_Copies  = new HashMap<Integer, Copy>();
        for(Copy c : S_Copies )
        {
            Map_Copies.put(c.getCopyid(), c);
        }

    }

    public static HashMap<Member, ArrayList<Item>> getReservationItemsByMember()
    {
        HashMap<Member, ArrayList<Item>> map = new HashMap<Member, ArrayList<Item>>();
        ArrayList<Item> items;
        for(Member m : LibraryGeneral.S_Members)
        {
            items = new ArrayList<Item>();
            for(Reservation r : m.reservations)
            {
                items.add(r.getItem());
            }
            if(items.size() >0)
                map.put(m, items);
       }
        return map;
    }

    public static HashMap<Book , ArrayList<Member>> getReservedBooks()
    {
        HashMap<Book , ArrayList<Member>> map = new HashMap<Book , ArrayList<Member>>();
        ArrayList<Member> members;
        for(Book b : LibraryGeneral.S_Books)
        {
            members = new ArrayList<Member>();
            for(Reservation r : b.reservations)
            {
                members.add(r.getMember());
            }
            
            if(members.size()>0)
                 map.put(b, members);
       }
        return map;
    }

    public static HashMap<Magazine , ArrayList<Member>> getReservedMagazines()
    {
        HashMap<Magazine , ArrayList<Member>> map = new HashMap<Magazine , ArrayList<Member>>();
        ArrayList<Member> members;
        for(Magazine b : LibraryGeneral.S_Magazines)
        {
            members = new ArrayList<Member>();
            for(Reservation r : b.reservations)
            {
                members.add(r.getMember());
            }
            if(members.size() > 0)
                map.put(b, members);
       }
        return map;
    }

    //get all copies that member borrow
    public static HashMap<Member, ArrayList<Copy>> getAllLoansByMember()
    {
        HashMap<Member, ArrayList<Copy>> map = new HashMap<Member, ArrayList<Copy>>();
        ArrayList<Copy> copies;
        for(Member m : LibraryGeneral.S_Members)
        {
            copies = new ArrayList<Copy>();
            for(Loan L : m.loans)
            {
                copies.add(L.getCopy());
            }
            if(copies.size() >0)
                map.put(m, copies);
       }
        return map;
    }

    /*
     * add new member
     */
    public static boolean addMember(String Name , String Address, String Phone)
    {
        int id=0;
       for(Member m : S_Members)
       {
           if(m.getNAme().equals(Name) )
               return false;

           if(id < m.getMemberID())
               id = m.getMemberID();
           
         
       }
       id += 1;
       Member newMember = new Member(id, Name, Address, Phone);
       S_Members.add(newMember);
       Map_Members.put(id, newMember);
       return true;
    }

    /*
     * add Book
     * return 0 if can't add book
     * return 1 if this is a copy of book
     * return 1 it it's new book and this is the first copy
     */
    public static boolean addBook(String Title , int DaysLength, String ISBN, String Authr)
    {


        //Book b = new Book(Phone, Checkoutlength, Name, Phone)
        int id=0;
        Copy newCopy = null;
       for(Book b : S_Books)
       {
           if(b.getISBN().equals(ISBN) )
           {
               for(int i=0; i< b.Copies.size();i++)
               {
                   Copy c = (Copy)b.Copies.get(i);
                   int C_id = c.getCopyid();
                   if(id< C_id)
                       id = C_id;

               }
               id +=1;
               newCopy = new Copy(id, b);
               S_Copies.add(newCopy);
           }
       }
       //check copy it's new book
       if(newCopy == null)
       {

           Book book = new Book(Title, DaysLength, ISBN, Authr);
           S_Books.add(book);
           newCopy = new Copy(1, book);
           S_Copies.add(newCopy);
           Map_Books.put(ISBN, book);

       }
       return true;
    }


        /*
     * add magazine
     */
    public static boolean addMagazine(String Title , int DaysLength, String IssueNo)
    {

        int id=0;
        Copy newCopy = null;
        for(Magazine mag : S_Magazines)
        {
           if(mag.getIssueno().equals(IssueNo) )
           {
               for(int i=0; i< mag.Copies.size();i++)
               {
                   Copy c = (Copy)mag.Copies.get(i);
                   int C_id = c.getCopyid();
                   if(id < C_id)
                       id = C_id;

               }
               id +=1;
               newCopy = new Copy(id, mag);
               S_Copies.add(newCopy);
           }
       }
       //check copy it's new book
       if(newCopy == null)
       {
           Magazine newMagazine = new Magazine(Title, DaysLength, IssueNo);
           S_Magazines.add(newMagazine);
           newCopy = new Copy(1, newMagazine);
           S_Copies.add(newCopy);
           Map_Magazines.put(IssueNo, newMagazine);
       }
        
       return true;
    }

    public static DefaultTableModel GetCopyBy(String ISBN , String Issueno , String MemberID , String CopyID)
    {
        DefaultTableModel DTM = new DefaultTableModel();
        Object[] newrow;
        //rows = new Object[100][];

        //object{copyid , IssueDate, Return date, reservation date , member }
        //getalllones of member
        HashMap<Member, ArrayList<Copy>> memLoans = getAllLoansByMember();
        //getall member reservations
        HashMap<Member, ArrayList<Item>> memreservations = getReservationItemsByMember();

        //Book Serach
        HashMap<Book , ArrayList<Member>> books_Reservemembers = getReservedBooks();
        
        //Magazines
        HashMap<Magazine , ArrayList<Member>> magzen_Reservemembers = getReservedMagazines();
        
        if(MemberID.length() >0 )
        {
            try
            {
                int id = Integer.parseInt(MemberID);
                if(Map_Members.containsKey(id))
                {
                    Member member = Map_Members.get(id);
                    //check member in loans map
                    if(memLoans.containsKey(member))
                    {
                        for(Copy c : memLoans.get(member) )
                        {
                            newrow = new Object[]{c.getCopyid(), c.loan.getCheckoutDate(),null,member.getNAme() , c.IsAvailable()  };
                            DTM.addRow(newrow);
                        }
                        
                    }

                    /////////////////////////////////////////
                    //check member reservations
                     if(memreservations.containsKey(member))
                     {
                         for(Item item : memreservations.get(member))
                         {
                             for(Copy c : item.Copies )
                            {
                                 if(c.loan != null)
                                 {
                                    newrow = new Object[]{c.getCopyid(), c.loan.getCheckoutDate(),null,member.getNAme() , c.IsAvailable()  };
                                 }
                                 else
                                     newrow = new Object[]{c.getCopyid(), null, item.Availableon().get(0),member.getNAme() , c.IsAvailable()  };
                                
                                 DTM.addRow(newrow);
                            }

                         }


                     }
                    //check member in reservation map
                }
                
            }
            catch(Exception ex)
            {}
            
        }

        if(ISBN.length() > 0)
        {
            
            
        }
        if(Issueno.length() > 0 )
        {
            
        }

        if(CopyID.length() >0)
        {
            try
            {
                if(Map_Copies.containsKey(Integer.parseInt(CopyID)))
                {
                    Copy c = Map_Copies.get(Integer.parseInt(CopyID));
                            if(c.loan != null)
                                 {
                                    newrow = new Object[]{c.getCopyid(), c.loan.getCheckoutDate(),null,c.loan.getMember().getNAme() , c.IsAvailable()  };
                                 }
                                 else
                                     newrow = new Object[]{c.getCopyid(), null, c.getItem().Availableon().get(0),null , c.IsAvailable()  };
                }
            }
            catch(Exception ex)
            {

            }
        }


        //Magazine
        return DTM;
    }



}
